package org.example.adservice.controller;

import lombok.RequiredArgsConstructor;
import org.example.adservice.service.AdCategoryService;
import org.example.adservice.common.Result;
import org.example.pojo.AdCategory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 创建广告分类控制器
 */
@RestController
@RequestMapping("/api/categories")
@RequiredArgsConstructor
public class AdCategoryController {

    private final AdCategoryService adCategoryService;

    /**
     * 获取所有分类
     */
    @GetMapping
    public Result<List<AdCategory>> getAllCategories() {
        List<AdCategory> categories = adCategoryService.getAllCategories();
        return Result.success(categories);
    }

    /**
     * 根据ID获取分类
     */
    @GetMapping("/{id}")
    public Result<AdCategory> getCategoryById(@PathVariable Integer id) {
        AdCategory category = adCategoryService.getCategoryById(id);
        if (category == null) {
            return Result.error("分类不存在");
        }
        return Result.success(category);
    }

    /**
     * 创建分类
     */
    @PostMapping
    public Result<AdCategory> createCategory(@RequestBody AdCategory category) {
        AdCategory createdCategory = adCategoryService.createCategory(category);
        return Result.success(createdCategory);
    }

    /**
     * 更新分类
     */
    @PutMapping("/{id}")
    public Result<AdCategory> updateCategory(@PathVariable Integer id, @RequestBody AdCategory category) {
        AdCategory updatedCategory = adCategoryService.updateCategory(id, category);
        return Result.success(updatedCategory);
    }

    /**
     * 删除分类
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteCategory(@PathVariable Integer id) {
        boolean success = adCategoryService.deleteCategory(id);
        if (success) {
            return Result.success("分类删除成功");
        } else {
            return Result.error("分类删除失败");
        }
    }
}